home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue55 / Clinic / GridEgU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-03  |  1.9 KB  |  83 lines

  1. unit GridEgU;
  2.  
  3. interface
  4.  
  5. uses
  6.   WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Db, DBTables, Grids;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Grid: TStringGrid;
  12.     tblCustomer: TTable;
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.   public
  17.     { Public declarations }
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.DFM}
  26.  
  27. type
  28.   TRecordDesc = class(TObject)
  29.   public
  30.     CustNo,
  31.     State: String;
  32.   end;
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. var
  36.   List: TStrings;
  37.   RecordDesc: TRecordDesc;
  38.   Loop: Integer;
  39. begin
  40.   List := TStringList.Create;
  41.   try
  42.     tblCustomer.Open;
  43.     { Loop through table records }
  44.     while not tblCustomer.Eof do
  45.     begin
  46.       { Create new object instance }
  47.       RecordDesc := TRecordDesc.Create;
  48.       try
  49.         { Set up object data fields }
  50.         RecordDesc.CustNo := tblCustomer.FieldByName('CustNo').AsString;
  51.         RecordDesc.State := tblCustomer.FieldByName('State').AsString;
  52.         { Let string list look after object for a while }
  53.         List.AddObject(tblCustomer.FieldByName('Company').AsString, RecordDesc);
  54.       except
  55.         RecordDesc.Free;
  56.       end;
  57.       tblCustomer.Next
  58.     end;
  59.     tblCustomer.Close;
  60.     { Initialise grid }
  61.     Grid.RowCount := List.Count + 1;
  62.     Grid.ColCount := 3;
  63.     Grid.Cells[0, 0] := 'Customer No.';
  64.     Grid.Cells[1, 0] := 'Company';
  65.     Grid.Cells[2, 0] := 'State';
  66.     { Loop through string list setting up grid rows }
  67.     for Loop := 0 to List.Count - 1 do
  68.     begin
  69.       Grid.Cells[0, Loop + 1] := TRecordDesc(List.Objects[Loop]).CustNo;
  70.       Grid.Cells[1, Loop + 1] := List[Loop];
  71.       Grid.Cells[2, Loop + 1] := TRecordDesc(List.Objects[Loop]).State;
  72.     end;
  73.   finally
  74.     { Delete objects stored in list }
  75.     for Loop := 0 to List.Count - 1 do
  76.       List.Objects[Loop].Free;
  77.     { Delete list }
  78.     List.Free
  79.   end
  80. end;
  81.  
  82. end.
  83.